package cn.zhz.office.convert.word.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ZipUtil;
import cn.zhz.office.convert.pdf.util.PdfConvertUtil;
import fr.opensagres.poi.xwpf.converter.core.ImageManager;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

import static cn.zhz.office.convert.constant.Constant.HTML_IMAGE_PATH;

/**
 * @author ZHZ
 * @since 2023-01-30
 * word转换工具类
 */
public class WordConvertUtil {


    /**
     * 转为pdf
     *
     * @param wordFilePath 原始word文档路径
     * @param pdfFilePath  转换后的pdf文档路径
     */
    public static void toPdf(String wordFilePath, String pdfFilePath) throws IOException {
        WordConvertUtil.toPdf(FileUtil.getInputStream(wordFilePath), FileUtil.getOutputStream(pdfFilePath));
    }

    /**
     * 转为pdf
     *
     * @param wordFile
     * @param pdfFile
     */
    public static void toPdf(File wordFile, File pdfFile) throws IOException {
        WordConvertUtil.toPdf(FileUtil.getInputStream(wordFile), FileUtil.getOutputStream(pdfFile));
    }

    /**
     * 转为pdf
     *
     * @param wordInputStream
     * @param pdfOutputStream
     */
    public static void toPdf(InputStream wordInputStream, OutputStream pdfOutputStream) throws IOException {

        //读取word文档
        XWPFDocument document = new XWPFDocument(wordInputStream);
        //将word转成pdf
        PdfOptions options = PdfOptions.create();
        PdfConverter.getInstance().convert(document, pdfOutputStream, options);

    }

    /**
     * 转为html
     *
     * @param wordFilePath 原始word文档路径
     * @param htmlFilePath 转换后的html文档路径
     */
    public static void toHtml(String wordFilePath, String htmlFilePath) throws IOException {
        WordConvertUtil.toHtml(FileUtil.getInputStream(wordFilePath), FileUtil.getOutputStream(htmlFilePath));
    }

    /**
     * 转为html
     *
     * @param wordInputStream 原始word文档输入流
     * @param htmlFilePath    转换后的html文档路径
     */
    public static void toHtml(InputStream wordInputStream, String htmlFilePath) throws IOException {
        WordConvertUtil.toHtml(wordInputStream, FileUtil.getOutputStream(htmlFilePath));
    }

    /**
     * 转为html
     *
     * @param wordInputStream
     * @param htmlOutputStream
     * @throws IOException
     */
    public static void toHtml(InputStream wordInputStream, OutputStream htmlOutputStream) throws IOException {

        //读取word文档
        XWPFDocument document = new XWPFDocument(wordInputStream);
        //将word转成html
        XHTMLOptions options = XHTMLOptions.create();
        // 存放图片的文件夹
        String uuid = UUID.fastUUID().toString();
        String tmpDirPath = FileUtil.getTmpDirPath() + uuid + File.separator;
        String tmpHtmlPath = tmpDirPath + "html" + File.separator;
        File tmpHtmlFolder = FileUtil.mkdir(tmpHtmlPath);
        ImageManager imageManager = new ImageManager(tmpHtmlFolder, HTML_IMAGE_PATH);
        options.setImageManager(imageManager);

        XHTMLConverter.getInstance().convert(document, FileUtil.getOutputStream(tmpHtmlPath + uuid + ".html"), options);
        File zipFile = ZipUtil.zip(tmpHtmlFolder);
        FileUtils.copyFile(zipFile, htmlOutputStream);
        // 删除临时文件夹
        FileUtil.del(tmpDirPath);
    }

    /**
     * 转为image
     *
     * @param wordInputStream
     * @param imageOutputStream
     */
    public static void toImage(InputStream wordInputStream, OutputStream imageOutputStream) throws IOException {
        File tempPdfFile = FileUtil.createTempFile();
        // 先转换为pdf
        WordConvertUtil.toPdf(wordInputStream, FileUtil.getOutputStream(tempPdfFile));
        // 在转换为图片
        FileInputStream inputStream = FileUtils.openInputStream(tempPdfFile);
        PdfConvertUtil.toImage(inputStream, imageOutputStream);
        inputStream.close();
        tempPdfFile.delete();
    }

}
